package com.example.howard.picview.DateBase;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/8/19.
 */
public class GetImageDate {

    public List<Map<String, Object>> getImageFiles(Context context) {
        List<Map<String, Object>> imagedata = new ArrayList<Map<String, Object>>();
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
                MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?",
                new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED);


        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
                    .moveToNext()) {
                String name = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
                String suffix = name
                        .substring(name.length() - 4, name.length());
                String url = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

                long longtime = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
                Date d = new Date(longtime);
                String data1 = d.toString();
                String data2 = data1.substring(4, 7);
                String i = toNum(data2);
                String time = data1.substring(30,34)+"/" +i+"/"+ data1.substring(8, 10)+"/"+data1.substring(11,19);

                Map<String, Object> map = new HashMap<String, Object>();
                map.put("NAME", name);
                map.put("SUFFIX", suffix);
                map.put("URL", url);
                map.put("TIME",time);
                imagedata.add(map);
            }
        }

        return imagedata;
    }

    public String toNum(String str) {
        switch (str) {
            case "Jan":
                return "01";
            case "Feb":
                return "02";
            case "Mar":
                return "03";
            case "Apr":
                return "04";
            case "May":
                return "05";
            case "Jun":
                return "06";
            case "Jul":
                return "07";
            case "Aug":
                return "08";
            case "Sep":
                return "09";
            case "Oct":
                return "10";
            case "Nov":
                return "11";
            case "Dec":
                return "12";
        }
        return null;
    }
}